package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.AllianceService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * 联盟管理
 */
@RestController
@RequestMapping("/bk")
public class AllianceController {

    private static final Logger logger = LogManager.getLogger(AllianceController.class);
    
    @Resource
    private AllianceService allianceService;

    @LoginRequired
    @PostMapping("/addAlliance")
    @ApiOperation(value = "新增联盟")
    public JsonResult addAlliance(
            @ApiParam(value = "图片url") @RequestParam(value = "imgUrl",required = false) String imgUrl,
            @ApiParam(value = "联盟名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "关于联盟") @RequestParam(value = "about",required = false) String about
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("name", name);
            pageData.put("about", about);
            pageData.put("imgUrl", imgUrl);

            allianceService.addAlliance(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateAlliance")
    @ApiOperation(value = "更新联盟")
    public JsonResult updateAlliance(
            @ApiParam(value = "图片url") @RequestParam(value = "imgUrl",required = false) String imgUrl,
            @ApiParam(value = "联盟ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "联盟名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "关于联盟") @RequestParam(value = "about",required = false) String about
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("imgUrl", imgUrl);
            pageData.put("name", name);
            pageData.put("about", about);
            allianceService.updateAlliance(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteAlliance")
    @ApiOperation(value = "删除联盟")
    public JsonResult deleteAlliance(
            @ApiParam(value = "联盟ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            allianceService.deleteAlliance(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listAlliance")
    @ApiOperation(value = "获取联盟列表")
    public JsonResult listAlliance(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(allianceService.listAlliance(pageData));
            result.setResultNum(allianceService.listAllianceCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    // 联盟活动 begin
    @LoginRequired
    @PostMapping("/addAllianceActivity")
    @ApiOperation(value = "新增联盟活动")
    public JsonResult addAllianceActivity(
            @ApiParam(value = "联盟活动标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "联盟活动副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "联盟ID") @RequestParam(value = "allianceId",required = false) String allianceId,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("content", content);
            pageData.put("allianceId", allianceId);
            pageData.put("origin", origin);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);

            allianceService.addAllianceActivity(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateAllianceActivity")
    @ApiOperation(value = "更新联盟活动")
    public JsonResult updateAllianceActivity(
            @ApiParam(value = "联盟活动ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "联盟活动标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "联盟活动副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "联盟ID") @RequestParam(value = "allianceId",required = false) String allianceId,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("content", content);
            pageData.put("allianceId", allianceId);
            pageData.put("origin", origin);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            allianceService.updateAllianceActivity(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/deleteAllianceActivity")
    @ApiOperation(value = "删除联盟活动")
    public JsonResult deleteAllianceActivity(
            @ApiParam(value = "联盟活动ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            allianceService.deleteAllianceActivity(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableAllianceActivity")
    @ApiOperation(value = "禁用或启用联盟活动")
    public JsonResult disableOrEnableAllianceActivity(
            @ApiParam(value = "联盟活动ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            allianceService.disableOrEnableAllianceActivity(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listAllianceActivity")
    @ApiOperation(value = "获取联盟活动列表")
    public JsonResult listAllianceActivity(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(allianceService.listAllianceActivity(pageData));
            result.setResultNum(allianceService.listAllianceActivityCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    // 联盟活动 end



}
